<?php
/**
 * 检查"系统配置"的子菜单
 */

$host = '113.44.136.17';
$port = 3306;
$database = 'qiandao';
$username = 'qiandao';
$password = 'NGbks3HWx5PhSxDX';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 检查系统配置的子菜单 ===\n\n";
    
    // 检查"系统配置"的子菜单
    $stmt = $pdo->query("
        SELECT id, title, status, url, node, sort 
        FROM system_menu 
        WHERE pid = 2 
        ORDER BY sort DESC, id ASC
    ");
    $subMenus = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "【系统配置】(ID:2) 的子菜单：\n";
    
    if (empty($subMenus)) {
        echo "  ❌ 没有子菜单！\n";
        echo "\n这就是问题所在！\n";
        echo "MenuService::filter() 的逻辑：\n";
        echo "  - 如果菜单有子菜单，URL 会被设置为 #\n";
        echo "  - 如果菜单没有子菜单，且 URL 为 # 或空，菜单会被过滤掉\n";
        echo "  - 如果一级菜单的所有二级菜单都被过滤掉，一级菜单也会被过滤掉\n";
    } else {
        echo "  子菜单数量: " . count($subMenus) . "\n\n";
        
        $enabledCount = 0;
        foreach ($subMenus as $sub) {
            $status = $sub['status'] == 1 ? '启用' : '禁用';
            echo "  ├─ {$sub['title']} (ID:{$sub['id']}, STATUS:{$status}, SORT:{$sub['sort']})\n";
            echo "      URL: {$sub['url']}\n";
            echo "      NODE: {$sub['node']}\n";
            
            if ($sub['status'] == 1) {
                $enabledCount++;
            }
        }
        
        echo "\n  启用的子菜单数量: {$enabledCount}\n";
        
        if ($enabledCount == 0) {
            echo "  ❌ 所有子菜单都被禁用了！\n";
        }
    }
    
    // 显示完整的菜单树
    echo "\n\n【完整的系统管理菜单树】\n";
    
    $stmt = $pdo->query("
        SELECT id, pid, title, status, url, node, sort
        FROM system_menu
        WHERE id = 1 OR pid = 1 OR pid IN (SELECT id FROM system_menu WHERE pid = 1)
        ORDER BY 
            CASE WHEN id = 1 THEN 0 WHEN pid = 1 THEN 1 ELSE 2 END,
            sort DESC, id ASC
    ");
    $allMenus = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    $tree = [];
    foreach ($allMenus as $menu) {
        if ($menu['pid'] == 0) {
            $tree[$menu['id']] = $menu;
            $tree[$menu['id']]['children'] = [];
        }
    }
    
    foreach ($allMenus as $menu) {
        if ($menu['pid'] == 1) {
            $tree[1]['children'][$menu['id']] = $menu;
            $tree[1]['children'][$menu['id']]['children'] = [];
        }
    }
    
    foreach ($allMenus as $menu) {
        if ($menu['pid'] > 1 && isset($tree[1]['children'][$menu['pid']])) {
            $tree[1]['children'][$menu['pid']]['children'][] = $menu;
        }
    }
    
    foreach ($tree as $level1) {
        $status1 = $level1['status'] == 1 ? '启用' : '禁用';
        echo "{$level1['title']} (ID:{$level1['id']}, {$status1}, SORT:{$level1['sort']})\n";
        
        foreach ($level1['children'] as $level2) {
            $status2 = $level2['status'] == 1 ? '启用' : '禁用';
            echo "  ├─ {$level2['title']} (ID:{$level2['id']}, {$status2}, SORT:{$level2['sort']}, URL:{$level2['url']})\n";
            
            if (!empty($level2['children'])) {
                foreach ($level2['children'] as $level3) {
                    $status3 = $level3['status'] == 1 ? '启用' : '禁用';
                    echo "  │  └─ {$level3['title']} (ID:{$level3['id']}, {$status3}, SORT:{$level3['sort']}, URL:{$level3['url']})\n";
                }
            }
        }
    }
    
    echo "\n\n【解决方案】\n";
    echo "问题：系统配置 (ID:2) 的 URL 是 #，但可能没有启用的三级子菜单\n";
    echo "解决方案：确保系统配置下的所有子菜单都是启用状态\n\n";
    
    echo "执行以下 SQL：\n";
    echo "UPDATE system_menu SET status = 1 WHERE pid = 2;\n";
    
} catch (PDOException $e) {
    echo "数据库错误: " . $e->getMessage() . "\n";
}

